import 'package:flutter/material.dart';
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/rendering.dart';

class WidthUtils {
  // 获取Uint8List数据
  
  Future<Uint8List?> toPng(GlobalKey globalKey) async {
    try {
      RenderRepaintBoundary boundary = globalKey.currentContext!
          .findRenderObject()! as RenderRepaintBoundary;
      double dpr = window.devicePixelRatio; // 获取当前设备的像素比
      var image = await boundary.toImage(pixelRatio: dpr);
      ByteData? byteData = await image.toByteData(format: ImageByteFormat.png);
      Uint8List? pngBytes = byteData?.buffer.asUint8List();

//Image.memory(pngBytes, width: 200, height: 200,);
      return pngBytes; //这个对象就是图片数据
    } catch (e) {
      print(e);
    }
    return null;
  }
}
